package micro.beauty.gateway.swagger.config;

import micro.beauty.gateway.swagger.handler.SwaggerResourceHandler;
import micro.beauty.gateway.swagger.handler.SwaggerSecurityHandler;
import micro.beauty.gateway.swagger.handler.SwaggerUiHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

/**
* @Author:         Liuliya
* @CreateDate:     2020/10/9 20:13
*/
@Configuration
public class RouterFunctionConfig {

    private final SwaggerResourceHandler swaggerResourceHandler;
    private final SwaggerSecurityHandler swaggerSecurityHandler;
    private final SwaggerUiHandler swaggerUiHandler;

    public RouterFunctionConfig(SwaggerResourceHandler swaggerResourceHandler,
                                SwaggerSecurityHandler swaggerSecurityHandler,
                                SwaggerUiHandler swaggerUiHandler) {
        this.swaggerResourceHandler = swaggerResourceHandler;
        this.swaggerSecurityHandler = swaggerSecurityHandler;
        this.swaggerUiHandler = swaggerUiHandler;
    }

    @Bean
    RouterFunction routerFunction() {
        return RouterFunctions
                .route(RequestPredicates.GET("/swagger-resources").and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui").and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security").and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler);
    }
}
